import { reqGetLogin, reqLogout } from "../../api/user"
const initUser = JSON.parse(localStorage.getItem("user")) || {
  nickName: "",
  token: "",
}
const state = initUser

const getters = {}
const actions = {
  async getLogin({ commit }, { phone, password }) {
    // if (!user) {
    //   commit("GET_LOGIN", null)
    //   return
    // }
    const user = await reqGetLogin(phone, password)
    commit("GET_LOGIN", user)
  },
  async logout({ commit }) {
    await reqLogout()
    commit("LOGOUT")
  },
}
const mutations = {
  GET_LOGIN(state, user) {
    const { nickName, token } = user
    // if (nickName) {
    localStorage.setItem("user", JSON.stringify({ nickName, token }))
    // } else {
    //   state.userInfo.userName = localStorage.getItem(state.flag)
    //   return
    // }
    state.nickName = nickName
    state.token = token
  },
  LOGOUT(state) {
    localStorage.removeItem("user")
    state.nickName = ""
    state.token = ""
  },
}

export default {
  namespaced: true,
  mutations,
  actions,
  getters,
  state,
}
